﻿using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.UI.Base;
using Habanero.UI.Win;
using NUnit.Framework;
using UseCaseManager.BO;
using UseCaseManager.Test.BO;
using UseCaseManager.UI;

namespace UseCaseManager.Test.UI
{
    [TestFixture]
    public class TestDropCompletionControl
    {
        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //Executes once for all tests in this fixture
            ClassDef.ClassDefs.Clear();
            ClassDef.LoadClassDefs(new XmlClassDefsLoader(BOBroker.GetClassDefsXml(), new DtdLoader()));
        }

        [SetUp]
        public void TestSetup()
        {
            //Test Setup Runs prior to each test in this fixture executing.
            BORegistry.DataAccessor = new DataAccessorInMemory();
            TestUtils.ClearAllTables();
            BusinessObjectManager.Instance.ClearLoadedObjects();
            TestUtils.WaitForGC();
            GlobalUIRegistry.ControlFactory = new ControlFactoryWin();
        }

        [Test]
        public void Test_CreateDropCompletionControl()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DropCompletionControl dropPlanner = new DropCompletionControl();
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropPlanner);
            Assert.IsNotNull(dropPlanner.DropSelector);
            Assert.IsNotNull(dropPlanner.UseCaseCompletedControl);
        }

        [Test]
        public void Test_CreateUseCaseCompletedControl_BorderLayoutManagerSetup()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            const int expectedOverallWidth = 550;
            const int expectedHeight = 355;
            const int expectedDropSelectorWidth = 350;
            DropCompletionControl dropCompletionControl = new DropCompletionControl { Width = expectedOverallWidth, Height = expectedHeight };
            //---------------Test Result -----------------------
            Assert.AreEqual(0, dropCompletionControl.DropSelector.Left);
            Assert.AreEqual(expectedDropSelectorWidth, dropCompletionControl.DropSelector.Width);
            Assert.AreEqual(expectedHeight, dropCompletionControl.DropSelector.Height);
            Assert.AreEqual(expectedHeight, dropCompletionControl.UseCaseCompletedControl.Height);
            Assert.AreEqual(expectedOverallWidth - expectedDropSelectorWidth, dropCompletionControl.UseCaseCompletedControl.Width);
        }

        [Test]
        public void Test_UseCaseCompletedControl_ChangeSize_MultiSelectorChangesSize()
        {
            //---------------Set up test pack-------------------
            DropCompletionControl dropCompletionControl = new DropCompletionControl { Width = 644, Height = 777 };
            const int expectedWidth = 544;
            const int expectedHeight = 877;
            const int expectedDropSelectorWidth = 350;
            //---------------Assert Precondition----------------
            Assert.AreEqual(expectedDropSelectorWidth, dropCompletionControl.DropSelector.Width);
            Assert.AreNotEqual(expectedHeight, dropCompletionControl.DropSelector.Height);
            Assert.AreNotEqual(expectedHeight, dropCompletionControl.UseCaseCompletedControl.Height);
            Assert.AreNotEqual(expectedWidth - expectedDropSelectorWidth, dropCompletionControl.UseCaseCompletedControl.Width);
            //---------------Execute Test ----------------------
            dropCompletionControl.Width = expectedWidth;
            dropCompletionControl.Height = expectedHeight;
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedHeight, dropCompletionControl.DropSelector.Height);
            Assert.AreEqual(expectedHeight, dropCompletionControl.UseCaseCompletedControl.Height);
            Assert.AreEqual(expectedWidth - expectedDropSelectorWidth, dropCompletionControl.UseCaseCompletedControl.Width);
        }

        [Test]
        public void Test_LoadsListOfZeroDrops()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            DropCompletionControl dropCompletionControl = new DropCompletionControl();
            DropsGridControlShowsRelease dropSelector = dropCompletionControl.DropSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dropCol.Count);
            //---------------Execute Test ----------------------
            dropCompletionControl.DropCol = dropCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropCompletionControl.DropCol);
            Assert.AreEqual(0, dropSelector.GridSelector.Grid.RowCount);
        }

        [Test]
        public void Test_LoadsListOfOneDrop()
        {
            //---------------Set up test pack-------------------
            Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            dropCol.Add(drop);
            DropCompletionControl dropCompletionControl = new DropCompletionControl();
            DropsGridControlShowsRelease dropSelector = dropCompletionControl.DropSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dropCol.Count);
            //---------------Execute Test ----------------------
            dropCompletionControl.DropCol = dropCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropCompletionControl.DropCol);
            Assert.AreEqual(1, dropSelector.GridSelector.Grid.RowCount);
            Assert.AreSame(drop, dropSelector.GridSelector.SelectedBusinessObject);
        }

        [Test]
        public void Test_LoadsListOfTwoDrops()
        {
            //---------------Set up test pack-------------------
            Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
            Drop drop2 = TestUtilsDrop.CreateUnsavedValidDrop();
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            dropCol.Add(drop);
            dropCol.Add(drop2);
            DropCompletionControl dropCompletionControl = new DropCompletionControl();
            DropsGridControlShowsRelease dropSelector = dropCompletionControl.DropSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dropCol.Count);
            //---------------Execute Test ----------------------
            dropCompletionControl.DropCol = dropCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropCompletionControl.DropCol);
            Assert.AreEqual(2, dropSelector.GridSelector.Grid.RowCount);
            Assert.AreSame(drop, dropSelector.GridSelector.SelectedBusinessObject, "First item should be selected");
        }

        [Test]
        public void Test_ResetDropsNullClearsGrid()
        {
            //---------------Set up test pack-------------------
            Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            dropCol.Add(drop);
            dropCol.Add(TestUtilsDrop.CreateUnsavedValidDrop());
            DropCompletionControl dropCompletionControl = new DropCompletionControl();
            DropsGridControlShowsRelease dropSelector = dropCompletionControl.DropSelector;
            dropCompletionControl.DropCol = dropCol;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dropCol.Count);
            Assert.AreEqual(2, dropSelector.GridSelector.Grid.RowCount);
            //---------------Execute Test ----------------------
            dropCompletionControl.DropCol = null;
            //---------------Test Result -----------------------
            Assert.AreEqual(0, dropSelector.GridSelector.Grid.RowCount);
        }

        ////these tests will be modified to account for new carryover functionality
        //[Test]
        //public void Test_LoadsListOfDrops_AndUseCases()
        //{
        //    //---------------Set up test pack-------------------
        //    Drop drop = TestUtilsDrop.CreateUnsavedDrop_WUseCases(TestUtils.GetRandomInt(0,6));
        //    BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
        //    dropCol.Add(drop);
        //    DropCompletionControl dropCompletionControl = new DropCompletionControl();
        //    DropsGridControl dropSelector = dropCompletionControl.DropSelector;
        //    UseCaseCompletedSelectorControl multiSelector = dropCompletionControl.UseCaseCompletedControl;
        //    //---------------Assert Precondition----------------
        //    Assert.AreEqual(1, dropCol.Count);
        //    Assert.AreEqual(0, dropSelector.GridSelector.Grid.RowCount);
        //    Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.AllOptions.Count);
        //    Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
        //    //---------------Execute Test ----------------------
        //    dropCompletionControl.DropCol = dropCol;
        //    //---------------Test Result -----------------------
        //    Assert.IsNotNull(dropCompletionControl.DropCol);
        //    Assert.AreEqual(1, dropSelector.GridSelector.Grid.RowCount);
        //    Assert.AreEqual(drop.UseCases.Count, multiSelector.UseCaseMultiSelector.AllOptions.Count);
        //    Assert.AreEqual(drop.UseCases.Count, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
        //}

        //[UseCaseManager.Test]
        //public void Test_LoadsListOfDrops_TwoDrops_AndUseCases()
        //{
        //    //---------------Set up test pack-------------------
        //    Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
        //    BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
        //    dropCol.Add(drop);
        //    drop.UseCases.Add(TestUtilsUseCase.CreateUnsavedValidUseCase());
        //    drop.UseCases.Add(TestUtilsUseCase.CreateUnsavedValidUseCase());
        //    drop.Save();
        //    dropCol.SaveAll();
        //    dropCol.Add(TestUtilsDrop.CreateUnsavedValidDrop());
        //    DropPlanningControl dropPlanningControl = new DropPlanningControl();
        //    DropsGridControl dropSelector = dropPlanningControl.DropSelector;
        //    TestUtilsUseCase.CreateSavedUseCase();
        //    UseCasesSelectorControl multiSelector = dropPlanningControl.UseCaseSelectorCntrl;
        //    //---------------Assert Precondition----------------
        //    Assert.AreEqual(2, dropCol.Count);
        //    Assert.AreEqual(0, dropSelector.GridSelector.Grid.RowCount);
        //    Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.AllOptions.Count);
        //    Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
        //    Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.SelectedOptionsListBox.Items.Count);
        //    //---------------Execute Test ----------------------
        //    dropPlanningControl.DropCol = dropCol;
        //    //---------------Test Result -----------------------
        //    Assert.IsNotNull(dropPlanningControl.DropCol);
        //    Assert.AreEqual(2, dropSelector.GridSelector.Grid.RowCount);
        //    Assert.AreEqual(3, multiSelector.UseCaseMultiSelector.AllOptions.Count);
        //    Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
        //    Assert.AreEqual(2, multiSelector.UseCaseMultiSelector.SelectedOptionsListBox.Items.Count);
        //}

        //[UseCaseManager.Test]
        //public void Test_LoadsListOfDrops_TwoDrops_AndUseCases_ResetSelectedDrop()
        //{
        //    //---------------Set up test pack-------------------
        //    Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
        //    BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
        //    dropCol.Add(drop);
        //    drop.UseCases.Add(TestUtilsUseCase.CreateUnsavedValidUseCase());
        //    drop.UseCases.Add(TestUtilsUseCase.CreateUnsavedValidUseCase());
        //    drop.Save();
        //    dropCol.SaveAll();
        //    Drop drop2 = TestUtilsDrop.CreateUnsavedValidDrop();
        //    dropCol.Add(drop2);
        //    DropPlanningControl dropPlanningControl = new DropPlanningControl();
        //    DropsGridControl dropSelector = dropPlanningControl.DropSelector;

        //    TestUtilsUseCase.CreateSavedUseCase();
        //    UseCasesSelectorControl multiSelector = dropPlanningControl.UseCaseSelectorCntrl;
        //    dropPlanningControl.DropCol = dropCol;
        //    //---------------Assert Precondition----------------
        //    Assert.AreEqual(2, dropCol.Count);
        //    Assert.AreEqual(2, drop.UseCases.Count);
        //    Assert.AreEqual(2, dropSelector.GridSelector.Grid.RowCount);
        //    Assert.AreEqual(3, multiSelector.UseCaseMultiSelector.AllOptions.Count);
        //    Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
        //    Assert.AreEqual(2, multiSelector.UseCaseMultiSelector.SelectedOptionsListBox.Items.Count);
        //    //---------------Execute Test ----------------------
        //    dropPlanningControl.DropSelector.GridSelector.SelectedBusinessObject = drop2;
        //    //---------------Test Result -----------------------
        //    Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AllOptions.Count);
        //    Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
        //    Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.SelectedOptionsListBox.Items.Count);
        //}
    }
}